#ifndef __LINKLIST_H
#define __LINKLIST_H

typedef struct _ListNode_t {
    struct _ListNode_t *pNext, *pPrev;
} ListHead_t, ListNode_t;

#define LIST_HEAD(name) ListHead_t name = {&(name), &(name)};

static __inline void List_Add(ListHead_t *pHead, ListNode_t *pNode)
{
    pNode->pPrev        = pHead->pPrev;
    pNode->pNext        = pHead;
    pHead->pPrev->pNext = pNode;
    pHead->pPrev        = pNode;
}

static __inline void List_Del(ListHead_t *pHead, ListNode_t *pNode)
{
    if (pHead != pNode) {
        pNode->pPrev->pNext = pNode->pNext;
        pNode->pNext->pPrev = pNode->pPrev;
    }
}

// get the next node of the linked list
#define list_next(pHead, pNode)                   ((pNode)->pNext == (pHead) ? NULL : (pNode)->pNext)
// check if the linked list is empty
#define lIST_EMPET(pHead)                         (pHead)->pNext == (pHead)
// traverse the linked list
#define list_foreach_front(pHead, pNode)          for (pNode = (pHead)->pNext; pNode != (pHead); pNode = pNode->pNext)
//return to the structure form the address of its numbers
#define list_entry(pNode, structName, nemberName) (structName *)((char *)pNode - (sizeof(structName) - sizeof(nemberName)))

#endif  //__LINKLIST_H
